#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time   : 2021/6/17 23:17
# @Author : cjw
import os
import pytest
from pytest_task2.utils.log import logger

from pytest_task2.setting import YML_DATA_PATH
from pytest_task2.code.calculator import Calculator
from pytest_task2.utils.file_operator import YmlOperator

# 计算器加法数据
add_data = YmlOperator(os.path.join(YML_DATA_PATH, 'calc_add.yml')).data
# 计算器除法数据
div_data = YmlOperator(os.path.join(YML_DATA_PATH, 'calc_div.yml')).data


@pytest.fixture()
def setup_teardown():
    """函数级别的初始化和清理"""
    logger.info('开始计算...')
    yield
    logger.info('计算结束...')


@pytest.fixture(scope='class')
def setup_teardown_class():
    """类级别的初始化和清理"""
    logger.info('计算器用例开始执行')
    yield
    logger.info('计算器用例结束执行')


@pytest.fixture(scope='class')
def get_calc_object():
    return Calculator()


@pytest.fixture(scope='class',
                params=add_data['add_int']['datas'],
                ids=add_data['add_int']['ids'])
def get_add_int_data(request):
    """获取整数加法yml数据"""
    return request.param


@pytest.fixture(scope='class',
                params=add_data['add_float']['datas'],
                ids=add_data['add_float']['ids'])
def get_add_float_data(request):
    """获取小数加法yml数据"""
    return request.param


@pytest.fixture(scope='class',
                params=add_data['add_float_int']['datas'],
                ids=add_data['add_float_int']['ids'])
def get_add_float_int_data(request):
    """获取小数、整数加法yml数据"""
    return request.param


@pytest.fixture(scope='class',
                params=add_data['add_fail']['datas'],
                ids=add_data['add_fail']['ids'])
def get_add_fail_data(request):
    """获取加法异常yml数据"""
    return request.param


@pytest.fixture(scope='class',
                params=div_data['div_int']['datas'],
                ids=div_data['div_int']['ids'])
def get_div_int_data(request):
    """获取整数除法yml数据"""
    return request.param


@pytest.fixture(scope='class',
                params=div_data['div_float']['datas'],
                ids=div_data['div_float']['ids'])
def get_div_float_data(request):
    """获取小数除法yml数据"""
    return request.param


@pytest.fixture(scope='class',
                params=div_data['div_float_int']['datas'],
                ids=div_data['div_float_int']['ids'])
def get_div_float_int_data(request):
    """获取小数、整数除法yml数据"""
    return request.param


@pytest.fixture(scope='class',
                params=div_data['div_fail']['datas'],
                ids=div_data['div_fail']['ids'])
def get_div_fail_data(request):
    """获取除法异常yml数据"""
    return request.param


def pytest_collection_modifyitems(items):
    """
    测试用例收集完成时，将收集到的item的name和nodeid的中文显示在控制台上
    :param items: 收集到的所有用例
    :return:
    """
    for item in items:
        # name: 用例的名称，nodeid： 用例的路径
        item.name = item.name.encode('utf-8').decode('unicode_escape')
        item._nodeid = item.nodeid.encode('utf-8').decode('unicode_escape')
